home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
shufflc.exe
/
SHUFFELC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-11-06
|
8KB
|
217 lines
/* permute a list of items at random and assign the top N as specified on input
actually set up for in-memory operation on poker hands with poker scoring
w.howell CIS 70215,206 11/2/91
*/
#include <stdio.h>
#include <time.h>
#ifdef ANSIC
#include <stdlib.h>
#include <sys/timeb.h>
void main(int , char * *);
void shsort(int * , int * , int); /* sort of D.L. Shell */
int scoreit(int * , int * ); /* score 5-card poker hand */
void help(int , char * ); /* user help & exit */
#endif
#ifdef BSD43
typedef int time_t;
#include <sys/timeb.h>
#include <ctype.h>
void main( );
void shsort( ); /* sort of D.L. Shell */
int scoreit( ); /* score 5-card poker hand */
void help( ); /* user help & exit */
#endif
#define FALSE 0
#define TRUE ~FALSE
#define NRINTBL 52
char cards[NRINTBL][10] = {
"Two-HT", "Three-HT", "Four-HT", "Five-HT", "Six-HT", "Seven-HT", "Eight-HT",
"Nine-HT", "Ten-HT", "Jack-HT", "Queen-HT", "King-HT", "Ace-HT",
"Two-SP", "Three-SP", "Four-SP", "Five-SP", "Six-SP", "Seven-SP", "Eight-SP",
"Nine-SP", "Ten-SP", "Jack-SP", "Queen-SP", "King-SP", "Ace-SP",
"Two-DI", "Three-DI", "Four-DI", "Five-DI", "Six-DI", "Seven-DI", "Eight-DI",
"Nine-DI", "Ten-DI", "Jack-DI", "Queen-DI", "King-DI", "Ace-DI",
"Two-CL", "Three-CL", "Four-CL", "Five-CL", "Six-CL", "Seven-CL", "Eight-CL",
"Nine-CL", "Ten-CL", "Jack-CL", "Queen-CL", "King-CL", "Ace-CL" };
void main(argc, argv)
int argc;
char **argv;
{
int nrsel, nrtimes, nrtbl, i, index[NRINTBL], srtkey[NRINTBL],
score, denom[5];
char message[75];
unsigned seed;
struct timeb timestr;
time_t *seedtime;
/* random # seed is seconds past the hour + milliseconds*/
ftime(×tr);
seed = (unsigned) (timestr.time % 3600 * 1000 + timestr.millitm);
srand(seed);
/* how many times to shuffel & print */
if(argc > 1) nrtimes = atoi(argv[1]);
else nrtimes = 1; /* default is a single deal */
if(nrtimes == 0) help(1,argv[1]);
/* how many to pick ? */
if(argc > 2) nrsel = atoi(argv[2]);
else nrsel = 5; /* default is 5 card hand */
if(nrsel == 0) help(2, argv[2]);
/* how many to pick from ? */
nrtbl = NRINTBL;
if(nrtbl < nrsel) {
sprintf(message,"Can Not chose %d items from %d available",
nrsel, nrtbl);
help(3, message);
}
/* load sort index table with integers 0 thru nrtbl - 1 */
for(i=0; i<nrtbl; i++) index[i] = i;
/* shuffel & deal as many times as asked */
while(nrtimes--) {
/* load sort key table with random integers
NOTE: we don't re-load index -- similar to placing the dealt
cards back on the top of the deck before shuffeling again */
for(i=0; i<nrtbl; i++) srtkey[i] = rand();
/* now sort the table by the random numbers (ie. shuffel them) */
shsort(srtkey, index, nrtbl);
/* now score the hand of 5 cards */
if(nrsel == 5) {
score = scoreit(index, denom); printf("%d : ", score);
}
/* then simply print out the first nrsel items -- WHAT A deal??? */
for(i=0; i<nrsel; i++) printf("%s ", cards[index[i]]);
printf("\n");
}
}
void shsort(key, bag, nelem) /* shell sort of key -- rearrange bag */
int *key, nelem; /* integer key table contains nelem elements */
int *bag; /* bag is a pointer table to baggage which must be rearranged */
{
/* sort key table of nelem integers and re-arrange bag via method of D.L. Shell
"A High Speed Sorting Proceedure" comm. acm 2 (july 1959), 30-32
-- w.howell 1/1/90 */
int i, *ipt,*ibpt, j,*jpt,*jbpt,k, *lpt,*lbpt, m, t, nel, flag;
/* skip rearrangement of bag if nelem is negative */
if(nelem < 0) { flag = FALSE; nel = - nelem; }
else { flag = TRUE; nel = nelem; }
m = nel;
while( (m = m / 2) > 0) {
k = nel - m;
for(j = 0, jpt = key, jbpt = bag; j < k; j++, jpt++, jbpt++) {
i = j;
ipt = jpt; lpt = ipt + m;
ibpt = jbpt; lbpt = ibpt + m;
while(i >= 0) {
if(*ipt <= *lpt) break;
t = *ipt; *ipt = *lpt; *lpt = t;
if(flag) { t = *ibpt; *ibpt = *lbpt; *lbpt = t; }
i = i - m;
lpt = ipt; ipt = ipt - m;
lbpt = ibpt; ibpt = ibpt - m;
}
}
}
}
/* score the poker hand of 5 cards stored in ind -- return 0-9 score PLUS
sorted demonation list in value list (in case of tied scores) */
int scoreit(ind, valist)
int *ind, *valist;
{
int i, suit, value, nsuit[4], nvalue[13], imin, imax,
flush = FALSE, strght = TRUE, n_two_of = 0,
three_of = FALSE, four_of = FALSE;
for(i=0; i<4; i++) nsuit[i] = 0;
for(i=0; i<13; i++) nvalue[i] = 0;
for(i=0; i<5; i++) {
suit = ind[i] / 13; value = ind[i] % 13;
valist[i] = value;
nsuit[suit]++; nvalue[value]++;
}
/* straight */
shsort(valist, valist, -5);
/* wrap ace in front of duce ?? */
if(valist[0] == 0 && valist[4] == 12) imax = 4;
else imax = 5;
for(i=1; i<imax; i++) {
if( (valist[i] - valist[i-1]) != 1) strght = FALSE;
}
/* flush */
for(suit=0; suit < 4; suit++) {
if(nsuit[suit] == 5) flush = TRUE;
}
/* X_of_a_kind */
for(value=0; value < 13; value++) {
switch(nvalue[value]) {
case(2): n_two_of++; break;
case(3): three_of = TRUE; break;
case(4): four_of = TRUE; break;
default: break;
}
}
/* now decide score */
/* Using Hoyle's Games pg. 100 table here -- no logic to break down
ties between two hands which score the same, but sorted hand VALUES
are returned in valist. */
if(flush && strght) { /* did we get a ROYAL ??? */
if(valist[0] == 8 && valist[4] == 12) return 9; /* WOW!! ROYAL FLUSH */
else return 8; /* NOT BAD -- straight flush */
}
if(four_of) return 7; /* 4 of a kind */
if(three_of && n_two_of == 1) return 6; /* FULL HOUSE */
if(flush) return 5; /* normal flush */
if(strght) return 4; /* normal straignt */
if(three_of) return 3;
if(n_two_of == 2) return 2;
if(n_two_of == 1) return 1;
return 0;
}
void help(helpno, helptext)
int helpno;
char *helptext;
{
switch(helpno) {
case(1):
case(2):
fprintf(stderr,"Bad command-line argument %s\n", helptext);
break;
case(3):
fprintf(stderr,"%s\n", helptext);
default: break;
}
fprintf(stderr,"USAGE: shuffelc [no_times [hand_size] ]\n");
fprintf(stderr," Shuffels a 52 card deck and deals a hand of size\n");
fprintf(stderr," hand_size (default is 5). Shuffel & deal is repeated\n");
fprintf(stderr," no_times times (default is 1).\n");
fprintf(stderr," EXAMPLE: shuffelc\n");
fprintf(stderr," might give 3 : King-SP Four-CL Four-DI Eight-DI Four-HT\n");
fprintf(stderr," where 3-Fours rank #3 on Hoyle's scale of 0 (nothing)\n");
fprintf(stderr," to 9 (Royal Flush).\n");
exit(4);
}